package com.umeox.moto.watch.themes.dialog;


import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.umeox.moto.watch.themes.R;

public class NotificationDialog implements MainDialogInterface {
    private BottomSheetDialog dialog;
    private TextView mMsgView;
    private TextView mSecondTv;
    private TextView mThirdTv;
    private ImageView mIconView;
    private OnDismissListener mListener;

    private NotificationDialog(Context context, int layoutId) {
        dialog = BottomSheetDialog.createDialog(context).dimAmount(0.5f);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

        View v = LayoutInflater.from(dialog.getContext()).inflate(layoutId == 0 ? R.layout.notification_dialog : layoutId, null);
        dialog.setContentView(v);
        v.setOnClickListener(v12 -> {
            dismiss();
        });
        mMsgView = v.findViewById(R.id.tvMsg);
        mIconView = v.findViewById(R.id.iv_type);
        mSecondTv = v.findViewById(R.id.tv_dialog_second);
        mThirdTv = v.findViewById(R.id.tv_dialog_third);
        View clickableView = v.findViewById(R.id.view_clickable);
        if (clickableView == null) return;
        clickableView.setOnClickListener(v1 -> {
            if (mListener != null) {
                dismiss();
                mListener.onClick();

            }
        });

    }

    public static NotificationDialog create(Context context, int layoutId) {
        return new NotificationDialog(context, layoutId);
    }

    public NotificationDialog cancelable(boolean cancelable) {
        dialog.cancelable(cancelable);
        return this;
    }

    public NotificationDialog canceledOnTouchOutside(boolean cancel) {
        dialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public NotificationDialog icon(int resId) {
        mIconView.setImageResource(resId);
        return this;
    }

    public NotificationDialog message(int resId) {
        mMsgView.setText(resId);
        return this;
    }

    public NotificationDialog secondTv(String resId) {
        if (mSecondTv != null)
            mSecondTv.setText(resId);
        return this;
    }

    public NotificationDialog thirdTv(String resId) {
        if (mThirdTv != null)
            mThirdTv.setText(resId);
        return this;
    }

    public NotificationDialog message(CharSequence message) {
        mMsgView.setText(message);
        return this;
    }

    public void show() {
        dialog.show();
    }


    public boolean isShowing() {

        return dialog.isShowing();
    }


    public void dismiss() {
        dialog.dismiss();
    }

    public void dismissImmediately() {
        dialog.dismissImmediately();
    }

    public Window getWindow() {
        return dialog.getWindow();
    }


    public void setOnDismissListener(OnDismissListener listener) {
        mListener = listener;
        dialog.setOnDismissListener(dialog -> listener.onDismiss());
    }

    public interface OnDismissListener {
        void onDismiss();

        void onClick();
    }
}
